﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using SERIX.Classes;

namespace SERIX
{
    public partial class AcaoIndicador : Seguranca
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            base.OnLoad(e);
            if (!IsPostBack)
            {
                int anoLiberado = CPlanejamento.AnoPlanejamentoCorrente(this.usuAut.Login);
                int ano = DateTime.Now.Year;
                for (int i = ano - 4; i <= ano + 2; i++)
                {
                    string valor = i.ToString();
                    ddlAno.Items.Add(new ListItem(valor, valor, true));
                }
                ddlAno.SelectedValue = anoLiberado.ToString();
            }
        }

        protected void odsProgramas_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            //if (string.IsNullOrEmpty(txtNomePrograma.Attributes["chave"]))
            //{
            //    e.Cancel = true;
            //    btnConfirmar.Visible = false;
            //}
            //else
            //    btnConfirmar.Visible = true;

            e.InputParameters["idt_prog"] = ddlProgramas.SelectedValue;
            e.InputParameters["login"] = UsuAut.Login;

            lblmsg.Visible = false;
        }

        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                decimal programa = (decimal)(((DataRowView)e.Row.DataItem))["ACAO"];
                if (programa == 1)
                    ((CheckBox)e.Row.FindControl("cbxProgramado")).Checked = true;
            }
        }

        protected void btnConfirmar_Click(object sender, EventArgs e)
        {
            //int idt_prog = Convert.ToInt32(txtNomePrograma.Attributes["chave"]);
            int idt_acao = Convert.ToInt32(ddlAcao.SelectedValue);
            int ANO_AIS = Convert.ToInt32(ddlAno.SelectedValue);
            int idt_Prog = Convert.ToInt32(ddlProgramas.SelectedValue);
            List<int> idt_Ind = new List<int>();
            List<int> idt_IndExcluir = new List<int>();
            foreach (GridViewRow row in gridView.Rows)
            {
                if (gridView.DataKeys[row.RowIndex]["ACAO"].ToString() == "0")//Não veio do banco
                {
                    if (((CheckBox)row.FindControl("cbxProgramado")).Checked)
                        idt_Ind.Add(Convert.ToInt32(gridView.DataKeys[row.RowIndex]["idt_ind"]));
                }
                else
                {
                    if (!((CheckBox)row.FindControl("cbxProgramado")).Checked)
                        idt_IndExcluir.Add(Convert.ToInt32(gridView.DataKeys[row.RowIndex]["idt_ind"]));
                }
            }
            CAcaoIndicador.RegistroEmLote(idt_Prog, idt_acao, idt_Ind, idt_IndExcluir, ANO_AIS, UsuAut.Login);
            lblmsg.Text = "Operação realizada com sucesso";
            lblmsg.Visible = true;
        }

        protected void odsAcao_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            //if (string.IsNullOrEmpty(txtNomePrograma.Attributes["chave"]))
            //    e.InputParameters["idt_prog"] = "0";
            //else
            //    e.InputParameters["idt_prog"] = txtNomePrograma.Attributes["chave"];
        }        

        protected void odsClasses_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            e.InputParameters["login"] = usuAut.Login;
        }

        protected void ddlClasses_SelectedIndexChanged(object sender, EventArgs e)
        {
            gridView.DataBind();
            lblmsg.Visible = false;
        }

        protected void ddlAcao_SelectedIndexChanged(object sender, EventArgs e)
        {
            gridView.DataBind();
            lblmsg.Visible = false;
        }

        protected void odsProgramas_Selected(object sender, ObjectDataSourceStatusEventArgs e)
        {
            if (e.ReturnValue != null && ((DataTable)e.ReturnValue).Rows.Count > 0)
            {
                btnConfirmar.Enabled = true;
            }
            else
                btnConfirmar.Enabled = false;
        }

        protected void cbxProgramadoHeader_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox cbx = (CheckBox)sender;
            bool check = false;
            if (cbx.Checked)
                check = true;
            foreach (GridViewRow row in gridView.Rows)
            {
                ((CheckBox)row.FindControl("cbxProgramado")).Checked = check;
            }
        }

        protected void ddlAno_SelectedIndexChanged(object sender, EventArgs e)
        {
            gridView.DataBind();
            int ANO_AIS = Convert.ToInt32(ddlAno.SelectedValue);
            if (!CPlanejamento.AnoLiberadoPlanejamento(ANO_AIS, UsuAut.Login))
            {
                gridView.Enabled = false;
                lblmsg.Visible = true;
                lblmsg.Text = "Modificação do Planejamento bloqueado para esse ano.";
            }
            else
            {
                gridView.Enabled = true;
                lblmsg.Visible = false;
            }
        }

        protected void ddlProgramas_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlAcao.DataBind();
            gridView.DataBind();
        }

    }
}
